package org.example;

import com.alibaba.fastjson.JSON;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.example.entity.ParamBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;

public class Main {

    public static void main(String[] args) throws ParseException {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("https://erp.qizhishangke.com/api/Trade/TradeDownload/downTrade");
        postMethod.addRequestHeader("accept", "*/*");
        //设置Content-Type，此处根据实际情况确定
        postMethod.addRequestHeader("Content-Type", "application/json;charset=UTF-8");
        //必须设置下面这个Header
        //添加请求参数
        postMethod.addRequestHeader("token", "30f6f609614204a507edbe9204bc2b33$|goto");

        ParamBody paramBody = new ParamBody();
        paramBody.setTids("");
        paramBody.setTimeZone("1");
        paramBody.setDownloadType("1");
        paramBody.setStartTime("1710432000");
        paramBody.setEndTime("1714199400");

        for (int i = 328; i <= 328; i++) {
            paramBody.setShopId(i);
            System.out.println("抓单店铺的Id为：" + i);

            String paramBodyStr = JSON.toJSONString(paramBody);

            System.out.println(paramBodyStr);
            postMethod.setRequestBody(paramBodyStr);
            try {
                httpClient.executeMethod(postMethod);//发送请求
                InputStream is = postMethod.getResponseBodyAsStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                StringBuffer sb = new StringBuffer();
                String str = "";
                while ((str = br.readLine()) != null) {
                    sb.append(str);
                }
                String respMsg = sb.toString();
                System.out.println("返回的信息为：" + respMsg);
            } catch (HttpException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                postMethod.releaseConnection();
            }
        }
    }
}